package com.ny.service.impl;

import com.ny.mapper.LockerRentalMapper;
import com.ny.mapper.MemberInfoMapper;
import com.ny.domain.LockerRentalDO;
import com.ny.dto.LockerRentalDTO;
import com.ny.query.LockerRentalQuery;
import com.ny.service.LockerRentalService;
import java.lang.Boolean;
import java.lang.Override;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ny.exception.BusinessException;
import com.ny.domain.MemberInfoDO;

/**
 * 寄存租赁管理模块的实现
 *
 * @author MSI-NB
 * @date 2025-03-08 13:14:38
 */
@Service
public class LockerRentalServiceImpl implements LockerRentalService {

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private LockerRentalMapper lockerRentalMapper;

    @Override
    public Boolean addLockerRental(LockerRentalDTO lockerRentalDTO) {
        // 查询会员信息是否存在
        MemberInfoDO memberInfo = memberInfoMapper.selectById(lockerRentalDTO.getMemberId());
        if (memberInfo == null) {
            throw new BusinessException("会员不存在");
        }
        // 查询寄存柜是否已被占用
        LockerRentalDO existingRental = lockerRentalMapper.selectByLockerIdAndEndTime(lockerRentalDTO.getLockerId());
        if (existingRental != null) {
            throw new BusinessException("寄存柜已被占用");
        }
        // 保存租赁记录到数据库
        LockerRentalDO lockerRentalDO = new LockerRentalDO();
        lockerRentalDO.setMemberId(lockerRentalDTO.getMemberId());
        lockerRentalDO.setLockerId(lockerRentalDTO.getLockerId());
        lockerRentalDO.setRentalStartTime(lockerRentalDTO.getRentalStartTime());
        lockerRentalDO.setRentalEndTime(lockerRentalDTO.getRentalEndTime());
        int result = lockerRentalMapper.insert(lockerRentalDO);
        return result > 0;
    }

    @Override
    public Boolean cancelLockerRental(LockerRentalQuery lockerRentalQuery) {
        // 查询租赁记录是否存在
        LockerRentalDO lockerRentalDO = lockerRentalMapper.selectById(lockerRentalQuery.getRentalId());
        if (lockerRentalDO == null) {
            throw new BusinessException("租赁记录不存在");
        }
        // 取消租赁记录
        int result = lockerRentalMapper.deleteById(lockerRentalQuery.getRentalId());
        return result > 0;
    }

    @Override
    public List<LockerRentalDO> getMemberLockerRentals(LockerRentalQuery lockerRentalQuery) {
        // 查询会员信息是否存在
        MemberInfoDO memberInfo = memberInfoMapper.selectById(lockerRentalQuery.getMemberId());
        if (memberInfo == null) {
            throw new BusinessException("会员不存在");
        }
        // 查询指定会员的租赁记录
        List<LockerRentalDO> lockerRentalList = lockerRentalMapper.selectByMemberId(lockerRentalQuery.getMemberId());
        return lockerRentalList;
    }

    @Override
    public List<LockerRentalDO> getAllLockerRentals() {
        // 查询所有寄存柜的租赁记录
        List<LockerRentalDO> lockerRentalList = lockerRentalMapper.selectAll();
        return lockerRentalList;
    }

    @Override
    public Boolean updateLockerRental(LockerRentalDTO lockerRentalDTO) {
        // 查询租赁记录是否存在
        LockerRentalDO lockerRentalDO = lockerRentalMapper.selectById(lockerRentalDTO.getRentalId());
        if (lockerRentalDO == null) {
            throw new BusinessException("租赁记录不存在");
        }
        // 查询寄存柜是否已被占用（除了当前租赁记录）
        LockerRentalDO existingRental = lockerRentalMapper.selectByLockerIdAndEndTimeExcludingId(lockerRentalDTO.getRentalId(), lockerRentalDTO.getLockerId());
        if (existingRental != null) {
            throw new BusinessException("寄存柜已被占用");
        }
        // 更新租赁记录的结束时间
        lockerRentalDO.setRentalEndTime(lockerRentalDTO.getRentalEndTime());
        int result = lockerRentalMapper.updateById(lockerRentalDO);
        return result > 0;
    }
}
